package com.koron.plan.web.maintainplan.service.impl;

import com.koron.common.core.util.CodeTools;
import com.koron.plan.web.common.CommonUtils;
import com.koron.plan.web.maintainplan.bean.entity.PlanEquipment;
import com.koron.plan.web.maintainplan.mapper.PlanEquipMapper;
import com.koron.plan.web.maintainplan.service.PlanEquipService;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * (PlanEquipment)表服务实现类
 *
 * @author twcao
 * @since 2022-03-16 16:25:39
 */
@Service
public class PlanEquipServiceImpl implements PlanEquipService {

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public PlanEquipment queryById(SessionFactory factory, String id) {
        PlanEquipMapper mapper = factory.getMapper(PlanEquipMapper.class);
        return mapper.queryById(id);
    }

    /**
     * 查询多条数据
     * @param equipment 设备
     * @return 对象列表
     */
    @Override
    public List<PlanEquipment> queryList(SessionFactory factory, PlanEquipment equipment) {
        PlanEquipMapper mapper = factory.getMapper(PlanEquipMapper.class);
        return mapper.queryList(equipment);
    }

    /**
     * 新增数据
     * @param equipment 实例对象
     * @return 实例对象
     */
    @Override
    public PlanEquipment insert(SessionFactory factory, PlanEquipment equipment) {
        PlanEquipMapper mapper = factory.getMapper(PlanEquipMapper.class);
        mapper.insert(equipment);
        return equipment;
    }

    @Override
    public int saveOrUpdate(SessionFactory factory, List<PlanEquipment> equipments) {
        PlanEquipMapper mapper = factory.getMapper(PlanEquipMapper.class);
        CommonUtils.sorted(equipments);
        equipments.stream().forEach(row -> row.setId(StringUtils.isBlank(row.getId()) ? CodeTools.getCode32() : row.getId()));
        return mapper.insertOrUpdateBatch(equipments);
    }

    /**
     * 修改数据
     * @param equipment 实例对象
     * @return 实例对象
     */
    @Override
    public PlanEquipment update(SessionFactory factory, PlanEquipment equipment) {
        PlanEquipMapper mapper = factory.getMapper(PlanEquipMapper.class);
        mapper.update(equipment);
        return this.queryById(factory, equipment.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(SessionFactory factory, String id) {
        PlanEquipMapper mapper = factory.getMapper(PlanEquipMapper.class);
        return mapper.deleteById(id) > 0;
    }

    /**
     * @param factory
     * @param planId  计划id
     * @return 是否成功
     */
    @Override
    public boolean deleteByPlanId(SessionFactory factory, String planId) {
        PlanEquipMapper mapper = factory.getMapper(PlanEquipMapper.class);
        return mapper.deleteByPlanId(planId) > 0;
    }
}
